package dcutils.cards.standard;

/**
 * In the standard deck of playing cards, a single card may have a rank and a suite.<br/>
 * A card may be a special type (a joker.)<br/>
 * @author dca
 */
public class Card {
	private Rank rank;
	private Suit suit;
	private boolean special;
	private String specialName;
	private String specialText;

	public Card(Rank r, Suit s) {
		this.rank = r;
		this.suit = s;
		this.special = false;
		this.specialName = "";
		this.specialText = "";
	} // END constructor

	public Card(String name, String text) {
		this.rank = null;
		this.suit = null;
		this.special = true;
		this.specialName = name;
		this.specialText = text;
	} // END constructor

	public Rank getRank() {
		return this.rank;
	} // END getRank

	public Suit getSuit() {
		return this.suit;
	} // END getSuit

	public boolean isSpecial() {
		return this.special;
	} // END isSpecial

	public String getSpecialName() {
		return this.specialName;
	} // END getSpecialName

	public String getSpecialText() {
		return this.specialText;
	} // END getSpecialText

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer(512);
		buffer.append(this.getClass().getSimpleName()); buffer.append(" [");

		if(special) {
			buffer.append(String.format("name=%s, text=%s", specialName, specialText));
		} else {
			buffer.append(String.format("rank=%s, suit=%s", rank.getName(), suit.getName()));
		} // END if/else

		buffer.append("]");
		return buffer.toString();
	} // END toString

	@Override
	public boolean equals(Object obj) {
		if(null == obj) {
			return false;
		} else if(obj instanceof Card) {
			Card card = (Card)obj;
			return this.toString().equals(card.toString());
		} else {
			return false;
		} // END if/else
	} // END equals

	@Override
	public int hashCode() {
		int hashCode = 101;
		if(special) {
			hashCode = (11 * hashCode) + (null == specialName || specialName.isEmpty() ? -1 : specialName.hashCode());
			hashCode = (11 * hashCode) + (null == specialText || specialText.isEmpty() ? -1 : specialText.hashCode());
		} else {
			hashCode = (11 * hashCode) + (null == rank ? -1 : rank.hashCode());
			hashCode = (11 * hashCode) + (null == suit ? -1 : suit.hashCode());
		} // END if/else
		return hashCode;
	} // END hashCode
} // END class Card
